import PyInstaller.__main__
import shutil, os, subprocess, sys

# 如何生成/How to compile
# 安装7z，将其7z.exe配置环境变量。 / Install 7z and include 7z.exe in environment.
# 执行 python build.py
# 拷贝生成结果


class TextDungeon:
    version = "1.0.1 incomplete 1"

class WhereHelper:
    base = os.path.dirname(__file__)

    dist_where = os.path.realpath(os.path.join(
        base,
        "./dist/"
    ))

    build_where = os.path.realpath(os.path.join(
        base,
        "./build/"
    ))

    spec_where = os.path.realpath(os.path.join(
        base,
        "./textdungeon.spec"
    ))

    lang_where = os.path.realpath(os.path.join(
        base,
        "./lang/"
    ))

    lang_to = os.path.realpath(os.path.join(
        dist_where,
        "./lang/"
    ))

    zip_where = os.path.realpath(os.path.join(
        base,
        "./generated/"
    ))

    zip_file = os.path.realpath(os.path.join(
        zip_where,
        "v{}.zip".format(TextDungeon.version)
    ))

def build():
    # 生成
    PyInstaller.__main__.run([
        '--name' + "=" + "textdungeon",
        '--onefile',
        'app.py'
    ])

    # 复制资源文件
    if os.path.exists(WhereHelper.lang_to):
        shutil.rmtree(WhereHelper.lang_to)
    shutil.copytree(WhereHelper.lang_where, WhereHelper.lang_to)

    # 打包

    # 最终文件夹
    os.makedirs(WhereHelper.zip_where,exist_ok=True)

    command = "7z" + " " + 'a "{}" "{}\\*"'
    subprocess.run(command.format(WhereHelper.zip_file,WhereHelper.dist_where),shell=True)

if __name__ == "__main__":
    build()